from flask import Blueprint, render_template, session, request, redirect, url_for, jsonify
from project.connect_db import con_my_sql

user_page = Blueprint('user_page', __name__)

@user_page.route("/new_service", methods = ["POST"])
def new_service():
    username = session.get('username')

    equipment_problem = request.form.get("equipment_problem")
    equipment_type = request.form.get("equipment_type")
    user_contact = request.form.get("user_contact")

    code = "INSERT INTO users_service_not_finish (username, equipment_problem, equipment_type, user_contact) VALUES ('%s', '%s', '%s', '%s')" % (username, equipment_problem, equipment_type, user_contact)
    con_my_sql(code)

    return redirect(url_for('user_page'))

@user_page.route("/change_password", methods = ["POST"])
def change_password():
    original_password = request.form.get('original_password')
    new_password = request.form.get("new_password")
    new_password2 = request.form.get("new_password2")

    username = session.get('username')

    code = "SELECT password FROM users_info WHERE username = '%s'" % username

    cursor_ans = con_my_sql(code)
    cursor_select = cursor_ans.fetchall()

    if original_password != cursor_select[0]['password']:
        return "原密码错误"
    else:
        if new_password != new_password2:
            return "两次输入的新密码不一致，请重新输入"
        else:
            code = "UPDATE users_info SET password = '%s' WHERE username = '%s'" % (new_password, username)
            con_my_sql(code)

            session['original_password'] = new_password

            return redirect(url_for('user_page'))

@user_page.route("/show_no_finish", methods = ["POST"])
def show_no_finish():
    username = session.get('username')

    code = "SELECT username, equipment_problem, equipment_type,user_contact FROM users_service_not_finish WHERE username = '%s'" % username
    cursor_ans = con_my_sql(code)
    cursor_select = cursor_ans.fetchall()

    return jsonify(cursor_select)

@user_page.route("/show_finish", methods = ["POST"])
def show_finish():
    username = session.get('username')

    code = "SELECT username, equipment_problem, equipment_type,user_contact FROM users_service_finish WHERE username = '%s'" % username
    cursor_ans = con_my_sql(code)
    cursor_select = cursor_ans.fetchall()

    return jsonify(cursor_select)